package utils.ui.adapter;

import utils.ImageLoaderUtil;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

/**
 * @Todo:ViewHolder
 */
public class ViewHolder {
    private SparseArray<View> mViews;
    private View mContentView;
    private Context mContext;
    
    public ViewHolder(Context context, ViewGroup parent, View contentView,
            int resId) {
        this.mContext = context;
        mViews = new SparseArray<View>();
        mContentView = LayoutInflater.from(context).inflate(resId, null);
        mContentView.setTag(this);
    }
    
    public static ViewHolder get(Context context, ViewGroup parent,
            View contentView, int resId) {
        if (contentView == null) {
            return new ViewHolder(context, parent, contentView, resId);
        }
        return (ViewHolder) contentView.getTag();
    }
    
    @SuppressWarnings("unchecked")
    public <T> T getView(int id) {
        View v = mViews.get(id);
        if (v == null) {
            v = mContentView.findViewById(id);
            mViews.put(id, v);
        }
        return (T) mViews.get(id);
    }
    
    public void setText(int id, String text) {
        TextView tv = getView(id);
        tv.setText(text);
    }
    
    public void setText(int id, int resId) {
        TextView tv = getView(id);
        tv.setText(resId);
    }
    
    public void setTextSize(int id, int size) {
        TextView tv = getView(id);
        tv.setTextSize(size);
    }
    
    public void setTextColor(int id, int color) {
        TextView tv = getView(id);
        tv.setTextColor(color);
    }
    
    public void setTextBackGround(int id, int resId) {
        TextView tv = getView(id);
        tv.setBackgroundResource(resId);
    }
    
    public void setImage(int id, int resId) {
        ImageView image = getView(id);
        image.setImageResource(resId);
    }
    
    public void setImage(int id, Drawable drawable) {
        ImageView image = getView(id);
        image.setImageDrawable(drawable);
    }
    
    public void setImage(int id, String url) {
        ImageView image = getView(id);
        ImageLoaderUtil.getInstance(mContext).disPlayImage(url, image);
    }
    
    public void setImageWithRadio(int id, String url,
            ImageLoadingListener listener) {
        ImageView image = getView(id);
        ImageLoaderUtil.getInstance(mContext)
                .disPlayImage(url, image, listener);
    }
    
    public void setImage(String url, ImageView image, int onLoadingPic,
            int onFailPic, int onEmptyPic) {
        ImageLoaderUtil.getInstance(mContext).disPlayImage(url, image,
                onLoadingPic, onFailPic, onEmptyPic);
    }
    
    public void setTag(int id, Object tag) {
        ((View) getView(id)).setTag(tag);
    }
    
    public void setContentWidth(int width) {
        View v = getContentView();
        LayoutParams params = v.getLayoutParams();
        params.width = width;
        v.setLayoutParams(params);
    }
    
    public void setContentHeihgt(int height) {
        View v = getContentView();
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
        params.height = height;
        v.setLayoutParams(params);
    }
    
    public void setVisibility(int id, int visibility) {
        View view = getView(id);
        view.setVisibility(visibility);
    }
    
    public void setOnClickListner(int id, OnClickListener l) {
        ((View) getView(id)).setOnClickListener(l);
    }
    
    public View getContentView() {
        return mContentView;
    }
}
